package com.examplemartin.jpademo.controller.purcharse;


import com.examplemartin.jpademo.bean.AjaxResult;
import com.examplemartin.jpademo.pojo.po.UserPO;
import com.examplemartin.jpademo.pojo.po.good.GoodPO;
import com.examplemartin.jpademo.pojo.po.good.GoodUnit;
import com.examplemartin.jpademo.pojo.po.purchase.PurDetailPO;
import com.examplemartin.jpademo.pojo.po.purchase.PurPO;
import com.examplemartin.jpademo.pojo.po.repository.purchase.PurInPO;
import com.examplemartin.jpademo.pojo.po.sell.SellDetailPO;
import com.examplemartin.jpademo.pojo.po.sell.SellPO;
import com.examplemartin.jpademo.pojo.po.stock.RepoInfoPO;
import com.examplemartin.jpademo.pojo.po.stock.StockPO;
import com.examplemartin.jpademo.pojo.po.stock.SummGoodPO;
import com.examplemartin.jpademo.pojo.po.supplier.SupplierPO;
import com.examplemartin.jpademo.pojo.vo.PurVO;
import com.examplemartin.jpademo.pojo.vo.SellVO;
import com.examplemartin.jpademo.pojo.vo.repoinfo.StockInfoVO;
import com.examplemartin.jpademo.pojo.vo.repoinfo.SummRepoVO;
import com.examplemartin.jpademo.service.GoodService;
import com.examplemartin.jpademo.service.StockService;
import com.examplemartin.jpademo.service.SupplierService;
import com.examplemartin.jpademo.service.UserService;
import com.examplemartin.jpademo.service.pur.PurDetailService;
import com.examplemartin.jpademo.service.pur.PurchaseService;
import com.examplemartin.jpademo.service.purin.PurInService;
import com.examplemartin.jpademo.service.sell.SellService;
import com.examplemartin.jpademo.service.stock.SummGoodService;
import com.examplemartin.jpademo.util.SessionUtil;
import com.examplemartin.jpademo.util.UUIDUtile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Controller
@RequestMapping("/purchase")
public class PurchaseController {

    @Resource
    private PurchaseService purchaseService;

    @Resource
    private PurInService purInService;

    @Resource
    private PurDetailService purDetailService;

    @Resource
    private GoodService goodService;

    @Resource
    private UserService userService;

    @Resource
    private SupplierService supplierService;
    @Resource
    private SellService sellService;

    /**
     * 跳转到采购订单主页面 purchaselist.do
     * GET
     */
    @GetMapping("/purchaselist.do")
    public String purchaseListDo(@RequestParam(value = "status", required = false, defaultValue = "-999") Integer status,
                                 Model model) {
        model.addAttribute("title", "采购订单");


        ArrayList<PurVO> purVOArrayList = new ArrayList<>();
        ArrayList<PurVO> purVOrunning = new ArrayList<>();
        ArrayList<PurVO> purVOfinish = new ArrayList<>();
        // 准备中
        List<PurPO> byStatus = purchaseService.findByStatus(PurchaseService.PURPO_STATUS_SPARE);
        for (PurPO purPO : byStatus) {
            PurVO purVO = new PurVO();

            purVO.setId(purPO.getId());
            purVO.setCreateTime(purPO.getCreateTime());
            purVO.setUserPO(purPO.getUserPO());
            purVO.setName(purPO.getName());
            purVO.setNeed(purPO.getNeed());
            purVO.setStatus(purPO.getStatus());
            purVO.setUserPO(purPO.getUserPO());
            purVO.setSupplierPO(purPO.getSupplierPO());
            purVO.setComment(purPO.getComment());
            Float totalmoney = new Float(0f);
            List<PurDetailPO> purDetailPOList = purPO.getPurDetailPOList();
            for (PurDetailPO purDetailPO : purDetailPOList) {
                totalmoney = totalmoney + purDetailPO.getPrice() * purDetailPO.getGoodcount();
            }
            purVO.setSumPurDetail(totalmoney);
            purVOArrayList.add(purVO);

        }
        Collections.sort(purVOArrayList, new Comparator<PurVO>() {
            @Override
            public int compare(PurVO o1, PurVO o2) {
                return o1.getNeed().compareTo(o2.getNeed());
            }
        });

        // 执行中
        List<PurPO> running = purchaseService.findByStatus(PurchaseService.PURPO_STATUS_RUNNING);

        for (PurPO purPO : running) {
            PurVO purVO = new PurVO();

            purVO.setId(purPO.getId());
            purVO.setCreateTime(purPO.getCreateTime());
            purVO.setName(purPO.getName());
            purVO.setNeed(purPO.getNeed());
            purVO.setStatus(purPO.getStatus());
            purVO.setUserPO(purPO.getUserPO());
            purVO.setSupplierPO(purPO.getSupplierPO());
            purVO.setComment(purPO.getComment());
            Float totalmoney = new Float(0f);
            List<PurDetailPO> purDetailPOList = purPO.getPurDetailPOList();
            for (PurDetailPO purDetailPO : purDetailPOList) {
                totalmoney = totalmoney + purDetailPO.getPrice() * purDetailPO.getGoodcount();
            }
            purVO.setSumPurDetail(totalmoney);
            purVOrunning.add(purVO);

        }
        Collections.sort(purVOrunning, new Comparator<PurVO>() {
            @Override
            public int compare(PurVO o1, PurVO o2) {
                return o1.getNeed().compareTo(o2.getNeed());
            }
        });

        // 已完成
        List<PurPO> finish = purchaseService.findByStatus(PurchaseService.PURPO_STATUS_FINISH);
        for (PurPO purPO : finish) {
            PurVO purVO = new PurVO();

            purVO.setId(purPO.getId());
            purVO.setCreateTime(purPO.getCreateTime());
            purVO.setName(purPO.getName());
            purVO.setNeed(purPO.getNeed());
            purVO.setStatus(purPO.getStatus());
            purVO.setUserPO(purPO.getUserPO());
            purVO.setSupplierPO(purPO.getSupplierPO());
            purVO.setComment(purPO.getComment());
            Float totalmoney = new Float(0f);
            List<PurDetailPO> purDetailPOList = purPO.getPurDetailPOList();
            for (PurDetailPO purDetailPO : purDetailPOList) {
                totalmoney = totalmoney + purDetailPO.getPrice() * purDetailPO.getGoodcount();
            }
            purVO.setSumPurDetail(totalmoney);
            purVOfinish.add(purVO);

        }
        Collections.sort(purVOfinish, new Comparator<PurVO>() {
            @Override
            public int compare(PurVO o1, PurVO o2) {
                return o1.getNeed().compareTo(o2.getNeed());
            }
        });


        purVOArrayList.addAll(purVOrunning);
        purVOArrayList.addAll(purVOfinish);


        model.addAttribute("purlist", purVOArrayList);
        model.addAttribute("purstatusname", PurchaseService.PURPO_STATUS_NAME);
        model.addAttribute("purstatusval", PurchaseService.PURPO_STATUS_VALUE);

        return "purchase/purchaselist";


    }

    /**
     * GEt
     * 新增订单页面 addpurchase.do
     */
    @GetMapping("addpurchase.do")
    public String addPurchaseDo(Model model) {
        model.addAttribute("title", "新增订单");
        model.addAttribute("purno", UUIDUtile.engender("ORDER"));
        model.addAttribute("ctime", new Date());

        // model  -- 计量单位
        model.addAttribute("goodunitname", GoodUnit.UNIT_NAME);
        model.addAttribute("goodunitval", GoodUnit.UNIT_VALUE);


        model.addAttribute("supplierlist", supplierService.findAll());
        return "purchase/addpurchase";
    }

    /**
     * 通过ajax 生成 商品列表 /purchase/getGoodlist.ajax
     *
     * @param supplierPO
     *
     * @return
     */
    @PostMapping("/getGoodlist.ajax")
    @ResponseBody
    public AjaxResult getGoodlistAjax(@RequestBody SupplierPO supplierPO) {

        List<GoodPO> suppliergood = supplierService.findById(supplierPO.getId()).getGood();
        ArrayList<GoodPO> good = new ArrayList<>();
        for (GoodPO goodPO : suppliergood) {
            good.add(goodPO);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodlist", good);

        return AjaxResult.success(200, "", map);
    }

    /**
     * 保存订单
     *
     * @param purPO
     *
     * @return
     */
    @PostMapping("/savePurPO.ajax")
    @ResponseBody
    public AjaxResult savePurPOAjax(@RequestBody PurPO purPO) {

        PurPO save = new PurPO();

        UserPO loninUser = SessionUtil.getLoninUser();
        UserPO userById = userService.findUserById(loninUser.getId());


        // 订单
        save.setName(purPO.getName());
        save.setComment(purPO.getComment());
        save.setNeed(purPO.getNeed());
        save.setStatus(purPO.getStatus());
        save.setCreateTime(purPO.getCreateTime());
        save.setUserPO(userById);
        // 供应商
        SupplierPO supplierbyId = supplierService.findById(purPO.getSupplierPO().getId());
        save.setSupplierPO(supplierbyId);

        save = purchaseService.savePurPO(save);

        // 订单明细
        save.setPurDetailPOList(new ArrayList<>());
        List<PurDetailPO> purDetailPOList = purPO.getPurDetailPOList();
        for (PurDetailPO purDetailPO : purDetailPOList) {
            GoodPO goodbyId = goodService.findById(purDetailPO.getGoodPO().getId());
            PurDetailPO tmp = new PurDetailPO();
            tmp.setGoodcount(purDetailPO.getGoodcount());
            tmp.setGoodPO(goodbyId);
            tmp.setPrice(purDetailPO.getPrice());
            tmp.setPurPO(save);
            tmp = purDetailService.save(tmp);
            save.getPurDetailPOList().add(tmp);
        }

        // 保存订单
        PurPO ret = purchaseService.savePurPO(save);
        if (ret == null) {
            return AjaxResult.fail(500, "订单保存失败");
        }


        return AjaxResult.success(200, "", null);

    }

    /**
     * 订单编辑
     * "/purchase/editpur.do?purid="+id
     *
     * @param purid
     *
     * @return
     */
    @GetMapping("/editpur.do")
    public String editPurDo(Long purid, Model model) {
        model.addAttribute("title", "订单编辑");
        // model  -- 计量单位
        model.addAttribute("goodunitname", GoodUnit.UNIT_NAME);
        model.addAttribute("goodunitval", GoodUnit.UNIT_VALUE);

        // model - pur 订单
        PurPO byId = purchaseService.findById(purid);
        model.addAttribute("pur", byId);

        // model - goodlist 商品下拉菜单
        List<GoodPO> suppliergoodlist = supplierService.findById(byId.getSupplierPO().getId()).getGood(); // 供应商的商品
        List<PurDetailPO> purDetailPOList = byId.getPurDetailPOList(); // 订单明细
        // 1 去除 已有商品选项
        ArrayList<GoodPO> goodPOS = new ArrayList<>();

        boolean flag = false;
        for (GoodPO suppliergoodPO : suppliergoodlist) {
            Long id = suppliergoodPO.getId();
            flag = false;
            for (PurDetailPO po : purDetailPOList) {
                Long pogoodid = po.getGoodPO().getId();
                if (pogoodid.longValue() == id.longValue()) {
                    flag = true;
                    break;
                }

            }
            if (!flag) {
                goodPOS.add(suppliergoodPO);
            }
        }


        model.addAttribute("goodlist", goodPOS);
        model.addAttribute("allgoodlist", suppliergoodlist);

        // model - 订单状态栏
        model.addAttribute("purstatusname", PurchaseService.PURPO_STATUS_NAME);
        model.addAttribute("purstatusval", PurchaseService.PURPO_STATUS_VALUE);
        //

        return "purchase/editpurchase";
    }

    /**
     * 采购订单审批界面
     *
     * @param model
     *
     * @return
     */
    @GetMapping("/auditpurchase.do")
    public String auditPurchaseDo(Model model) {
        model.addAttribute("title", "订单审核");


        // model  -- 计量单位
        model.addAttribute("goodunitname", GoodUnit.UNIT_NAME);
        model.addAttribute("goodunitval", GoodUnit.UNIT_VALUE);


        ArrayList<PurVO> purVOArrayList = new ArrayList<>();
        ArrayList<PurVO> purVOrunning = new ArrayList<>();
        ArrayList<PurVO> purVOfinish = new ArrayList<>();
        // 准备中
        List<PurPO> byStatus = purchaseService.findByStatus(PurchaseService.PURPO_STATUS_SPARE);
        for (PurPO purPO : byStatus) {
            PurVO purVO = new PurVO();

            purVO.setId(purPO.getId());
            purVO.setCreateTime(purPO.getCreateTime());
            purVO.setName(purPO.getName());
            purVO.setNeed(purPO.getNeed());
            purVO.setStatus(purPO.getStatus());
            purVO.setUserPO(purPO.getUserPO());
            purVO.setSupplierPO(purPO.getSupplierPO());
            purVO.setComment(purPO.getComment());
            Float totalmoney = new Float(0f);
            List<PurDetailPO> purDetailPOList = purPO.getPurDetailPOList();
            for (PurDetailPO purDetailPO : purDetailPOList) {
                totalmoney = totalmoney + purDetailPO.getPrice() * purDetailPO.getGoodcount();
            }
            purVO.setSumPurDetail(totalmoney);
            purVOArrayList.add(purVO);

        }
        Collections.sort(purVOArrayList, new Comparator<PurVO>() {
            @Override
            public int compare(PurVO o1, PurVO o2) {
                return o1.getNeed().compareTo(o2.getNeed());
            }
        });

        // 执行中
        List<PurPO> running = purchaseService.findByStatus(PurchaseService.PURPO_STATUS_RUNNING);

        for (PurPO purPO : running) {
            PurVO purVO = new PurVO();

            purVO.setId(purPO.getId());
            purVO.setCreateTime(purPO.getCreateTime());
            purVO.setName(purPO.getName());
            purVO.setNeed(purPO.getNeed());
            purVO.setStatus(purPO.getStatus());
            purVO.setUserPO(purPO.getUserPO());
            purVO.setSupplierPO(purPO.getSupplierPO());
            purVO.setComment(purPO.getComment());
            Float totalmoney = new Float(0f);
            List<PurDetailPO> purDetailPOList = purPO.getPurDetailPOList();
            for (PurDetailPO purDetailPO : purDetailPOList) {
                totalmoney = totalmoney + purDetailPO.getPrice() * purDetailPO.getGoodcount();
            }
            purVO.setSumPurDetail(totalmoney);
            purVOrunning.add(purVO);

        }
        Collections.sort(purVOrunning, new Comparator<PurVO>() {
            @Override
            public int compare(PurVO o1, PurVO o2) {
                return o1.getNeed().compareTo(o2.getNeed());
            }
        });

        // 已完成
        List<PurPO> finish = purchaseService.findByStatus(PurchaseService.PURPO_STATUS_FINISH);
        for (PurPO purPO : finish) {
            PurVO purVO = new PurVO();

            purVO.setId(purPO.getId());
            purVO.setCreateTime(purPO.getCreateTime());
            purVO.setName(purPO.getName());
            purVO.setNeed(purPO.getNeed());
            purVO.setStatus(purPO.getStatus());
            purVO.setUserPO(purPO.getUserPO());
            purVO.setSupplierPO(purPO.getSupplierPO());
            purVO.setComment(purPO.getComment());
            Float totalmoney = new Float(0f);
            List<PurDetailPO> purDetailPOList = purPO.getPurDetailPOList();
            for (PurDetailPO purDetailPO : purDetailPOList) {
                totalmoney = totalmoney + purDetailPO.getPrice() * purDetailPO.getGoodcount();
            }
            purVO.setSumPurDetail(totalmoney);
            purVOfinish.add(purVO);

        }
        Collections.sort(purVOfinish, new Comparator<PurVO>() {
            @Override
            public int compare(PurVO o1, PurVO o2) {
                return o1.getNeed().compareTo(o2.getNeed());
            }
        });


        purVOArrayList.addAll(purVOrunning);
        purVOArrayList.addAll(purVOfinish);


        model.addAttribute("purlist", purVOArrayList);
        model.addAttribute("purstatusname", PurchaseService.PURPO_STATUS_NAME);
        model.addAttribute("purstatusval", PurchaseService.PURPO_STATUS_VALUE);
        model.addAttribute("pur_status_map", PurchaseService.PUR_STATUS_MAP);

        return "purchase/auditpurchase";
    }

    /**
     * auditpurchasedetail.do
     * 跳转到审核明细页面
     */
    @GetMapping("/auditpurchasedetail.do")
    public String auditpurchasedetailDo(Long purid, Model model) {
        model.addAttribute("title", "订单审核");

        // model - pur 订单
        PurPO byId = purchaseService.findById(purid);
        model.addAttribute("pur", byId);

        // model - goodlist 商品下拉菜单
        List<GoodPO> suppliergood = supplierService.findById(byId.getSupplierPO().getId()).getGood();
        // 1 去除 已有商品选项
        ArrayList<GoodPO> goodPOS = new ArrayList<>();

        boolean flag = false;
        for (GoodPO suppliergoodPO : suppliergood) {
            Long id = suppliergoodPO.getId();
            flag = false;
            for (GoodPO po : suppliergood) {
                Long poid = po.getId();
                if (poid.longValue() == id.longValue()) {
                    flag = true;
                    break;
                }

            }
            if (!flag) {
                goodPOS.add(suppliergoodPO);
            }
        }


        model.addAttribute("goodlist", goodPOS);

        // model - 订单状态栏
        model.addAttribute("purstatusname", PurchaseService.PURPO_STATUS_NAME);
        model.addAttribute("purstatusval", PurchaseService.PURPO_STATUS_VALUE);
        model.addAttribute("pur_status_map", PurchaseService.PUR_STATUS_MAP);

        // 查询已入库明细数量
        Map<String, Object> getpurinlist = purchaseService.getpurinlist(purid);


        List<PurInPO> purInList = purInService.findAllByPurPO(byId);

        model.addAttribute("allpurinlist", purInList); // 所有入库单
        model.addAttribute("goodmap", getpurinlist.get("goodmap")); // 所有入库单


        return "purchase/auditpurchasedetail";
    }

    /**
     * 把订单 转换为 .... 状态
     * /purchase/auditPurPO.ajax
     *
     * @param purPO
     *
     * @return
     */
    @PostMapping("/auditPurPO.ajax")
    @ResponseBody
    public AjaxResult auditPurPOAjax(@RequestBody PurPO purPO) {

        AjaxResult ajaxResult = purchaseService.auditPurPOAjax(purPO);

        return ajaxResult;
    }

    /**
     * 保存修改 savemodifyPurPO.ajax
     *
     * @param purPO
     *
     * @return
     */
    @PostMapping("/savemodifyPurPO.ajax")
    @ResponseBody
    public AjaxResult savemodifyPurPOAjax(@RequestBody PurPO purPO) {
        PurPO save = purchaseService.findById(purPO.getId());
        // 删除明细
        List<PurDetailPO> orgpurDetailList = save.getPurDetailPOList();
        for (PurDetailPO purDetailPO : orgpurDetailList) {
            purDetailService.deleteById(purDetailPO.getId());
        }
        // 订单
        save.setName(purPO.getName());
        save.setComment(purPO.getComment());
        save.setNeed(purPO.getNeed());
        save.setStatus(purPO.getStatus());
        save.setCreateTime(purPO.getCreateTime());
        // 供应商
        SupplierPO supplierbyId = supplierService.findById(purPO.getSupplierPO().getId());
        save.setSupplierPO(supplierbyId);


        // 订单明细
        save.setPurDetailPOList(new ArrayList<>());


        save = purchaseService.savePurPO(save);

        List<PurDetailPO> purDetailPOList = purPO.getPurDetailPOList();
        save.setPurDetailPOList(new ArrayList<PurDetailPO>());
        for (PurDetailPO purDetailPO : purDetailPOList) {
            GoodPO goodbyId = goodService.findById(purDetailPO.getGoodPO().getId());
            PurDetailPO tmp = new PurDetailPO();
            tmp.setGoodcount(purDetailPO.getGoodcount());
            tmp.setGoodPO(goodbyId);
            tmp.setPrice(purDetailPO.getPrice());
            tmp.setPurPO(save);
            tmp = purDetailService.save(tmp);
            save.getPurDetailPOList().add(tmp);
        }

        // 保存订单
        PurPO ret = purchaseService.savePurPO(save);
        if (ret == null) {
            return AjaxResult.fail(500, "订单修改失败");
        }


        return AjaxResult.success(200, "订单修改成功", null);
    }

    @GetMapping("/shortagelist.do")
    public String shortagelistDo(Model model) {
        model.addAttribute("title", "缺货明细");

        // 缺货单
        List<SellPO> all = sellService.findByStatus(SellService.SELL_STATUS_LESSGOOG);
        ArrayList<SellVO> sellVOS = new ArrayList<>();
        for (SellPO sellPO : all) {
            SellVO vo = new SellVO();
            vo.setComment(sellPO.getComment());
            vo.setCreateTime(sellPO.getCreateTime());
            vo.setUpdateTime(sellPO.getUpdateTime());
            vo.setUserPO(sellPO.getUserPO());
            vo.setId(sellPO.getId());
            vo.setName(sellPO.getName());
            vo.setNeed(sellPO.getNeed());
            vo.setStatus(sellPO.getStatus());
            vo.setCustPO(sellPO.getCustPO());
            // 合计总价
            Float total = 0f;
            List<SellDetailPO> sellDetailPOList = sellPO.getSellDetailPOList();
            for (SellDetailPO sellDetailPO : sellDetailPOList) {
                total = total + sellDetailPO.getPrice() * sellDetailPO.getGoodcount();
            }
            vo.setSumSellDetail(total);
            sellVOS.add(vo);
        }

        model.addAttribute("selllist", sellVOS);
        model.addAttribute("sellstatus", SellService.SELL_STATUS_MAP);

        return "purchase/shortagelist";
    }

    @Resource
    private SummGoodService summGoodService;

    /**
     * 缺货单明细
     * /shortagedetail.do
     * @param sellid
     *
     * @return
     */
    @GetMapping("/shortagedetail.do")
    public String shortagedetailDo(Long sellid, Model model) {
        model.addAttribute("title", "缺货单明细");
// --------
        // model  -- 计量单位
        model.addAttribute("goodunitname", GoodUnit.UNIT_NAME);
        model.addAttribute("goodunitval", GoodUnit.UNIT_VALUE);

        // model - sell 订单
        SellPO byIdsell = sellService.findById(sellid);
        model.addAttribute("sell", byIdsell);

//        // model - goodlist 商品下拉菜单
////        List<RepoinfoVO> allGroupByGoodId = repoInfoService.findAllGroupByGoodId();
//        List<SummGoodPO> allGroupByGoodId = summGoodService.findAll();

        // 1 去除 已有商品选项
        ArrayList<GoodPO> goodPOS = new ArrayList<>();

//        for (SellDetailPO sellDetailPO : byIdsell.getSellDetailPOList()) {
//            goodPOS.add(sellDetailPO.getGoodPO());
//        }

// 库存
//        List<RepoinfoVO> allGroupGoodId = repoInfoService.findAllGroupByGoodId();
//        List<SummGoodPO> allGroupGoodId = summGoodService.findAll();
//        HashMap<Long, Integer> repomap = new HashMap<>();
//        for (SummGoodPO summGoodPO : allGroupByGoodId) {
//            repomap.put(summGoodPO.getGoodPO().getId(),summGoodPO.getGoodcount());
//        }
//        model.addAttribute("repomap", repomap);
//
//        boolean flag = false;
//
//        for (SummGoodPO summGoodPO : allGroupByGoodId) {
//            flag = false;
//            Long goodinrepo = summGoodPO.getGoodPO().getId();
//            List<SellDetailPO> sellDetailPOList = byIdsell.getSellDetailPOList();
//            for (SellDetailPO sellDetailPO : sellDetailPOList) {
//                if(sellDetailPO.getGoodPO().getId().longValue() ==goodinrepo.longValue()){
//                    flag=true;
//                    break;
//                }
//            }
//            if(!flag){
//                goodPOS.add(summGoodPO.getGoodPO());
//            }
//        }
//
//        model.addAttribute("goodlist", goodPOS);
//        model.addAttribute("allgoodlist", allGroupByGoodId);
        List<SummGoodPO> allGroupGoodId = summGoodService.findAll();
        List<SummGoodPO> allGroupByGoodId = summGoodService.findAll();
        HashMap<Long, Integer> repomap = new HashMap<>();
        for (SummGoodPO summGoodPO : allGroupByGoodId) {
            repomap.put(summGoodPO.getGoodPO().getId(),summGoodPO.getGoodcount());
        }
        model.addAttribute("repomap", repomap);

        model.addAttribute("sell_status_map", SellService.SELL_STATUS_MAP);
        // model - 订单状态栏
        model.addAttribute("purstatusname", PurchaseService.PURPO_STATUS_NAME);
        model.addAttribute("purstatusval", PurchaseService.PURPO_STATUS_VALUE);
//        ----------


        return "purchase/shortagedetail";
    }

    @Resource
    private StockService stockService;

    /**
     * 补货
     * @param model
     * @return
     */
    @GetMapping("/replenishlist.do")
    public String replenishDo(Model model){

        model.addAttribute("title","补货");

        List<RepoInfoPO> repoInfoPOS = stockService.findAllNeedReplenish( );
        model.addAttribute("repoInfoPOS", repoInfoPOS);
        model.addAttribute("stock", stockService.findAll());

//        List<SummGoodPO> allsumm = summGoodService.findAll();
//        ArrayList<SummRepoVO> summRepoVOS = new ArrayList<>();
//
//        for (SummGoodPO summGoodPO : allsumm) {
//            SummRepoVO summRepoVO = new SummRepoVO();
//            summRepoVO.setGoodPO(summGoodPO.getGoodPO());
//            summRepoVO.setGoodcount(summGoodPO.getGoodcount());
//            summRepoVO.setGoodspare(summGoodPO.getGoodspare());
//            //添加仓库存储信息
//            summRepoVO.setStockinfolist(new ArrayList<StockInfoVO>());
//            List<RepoInfoPO> repoInfoPOList = repoInfoService.findAllByGoodPO(summGoodPO.getGoodPO());
//            for (RepoInfoPO repoInfoPO : repoInfoPOList) {
//                StockPO stockPO = repoInfoPO.getStockPO();
//                GoodPO goodPO = repoInfoPO.getGoodPO();
//                Integer threshold = repoInfoPO.getThreshold();
//                Integer goodcount = repoInfoPO.getGoodcount();
//                StockInfoVO stockInfoVO = new StockInfoVO();
//                stockInfoVO.setStockPO(stockPO);
//                stockInfoVO.setGoodcount(goodcount);
//                stockInfoVO.setGoodPO(goodPO);
//                stockInfoVO.setGoodcount(goodcount);
//                stockInfoVO.setThreshold(threshold);
//                summRepoVO.getStockinfolist().add(stockInfoVO);
//            }
//            summRepoVOS.add(summRepoVO);
//        }
//        model.addAttribute("summlist", summRepoVOS);

        return "purchase/replenish";
    }
}
